import com.itheima.studentsystem.Student;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        // 定义集合
        ArrayList<Student> list = new ArrayList<>();

        loop:
        while (true) {
            System.out.println("============================欢迎使用学习管理系统============================");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");

            // 键盘录入用户的选择
            Scanner scanner = new Scanner(System.in);
            //
            System.out.println("请输入你的选择:");
            String choose = scanner.next();
            switch (choose) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("退出成功");
                    break loop;
                default:
                    System.out.println("没有该选项，请重新输入");
            }
        }
    }


    // 添加学生
    public static void addStudent(ArrayList<Student> list) {

        Student student = new Student();

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入id:");
            String id = scanner.next();
            boolean flag = contains(list, id);
            if (flag) {
                System.out.println("id重复，请重新输入。");
            } else {
                student.setId(id);
                break;
            }
        }

        System.out.println("请输入姓名:");
        String name = scanner.next();
        student.setName(name);

        System.out.println("请输入年龄:");
        int age = scanner.nextInt();
        student.setAge(age);

        System.out.println("请输入地址:");
        String address = scanner.next();
        student.setAddress(address);

        list.add(student);

        System.out.println("添加成功");
    }

    // 删除学生
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的学生id:");

        String id = scanner.next();
        // 查询id是否存在
        int index = getIndex(list, id);
        if (index >= 0) {
            list.remove(index);
            System.out.println("id为" + id + "的学生删除成功");
        } else {
            System.out.println("id不存在,删除失败");
        }

    }

    // 修改学生
    public static void updateStudent(ArrayList<Student> list) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的学生id:");
        String id = scanner.next();
        // 判断要删除的id是否存在
        int index = getIndex(list, id);
        if (index == -1) {
            System.out.println("要删除的学生不存在，请重新输入.");
            return;
        }
        // 如果存在
        Student student = list.get(index);

        System.out.println("请输入姓名:");

        String newName = scanner.next();
        student.setName(newName);

        System.out.println("请输入年龄:");
        int newAge = scanner.nextInt();
        student.setAge(newAge);

        System.out.println("请输入地址:");
        String newAddress = scanner.next();
        student.setAddress(newAddress);

        System.out.println("学生信息修改成功");
    }

    // 查询学生
    public static void queryStudent(ArrayList<Student> list) {

        // 判断如果为0
        if (list.isEmpty()) {
            System.out.println("没查询到学生信息，请添加之后再查询");
            return;
        }

        // 如果不为空
        // 打印表头
        System.out.println("ID" + "\t\t" + "姓名" + "\t" + "年龄" + "\t" + "家庭地址");
        for (Student student : list) {
            System.out.println(student.getId() + "\t\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getAddress());
        }

    }


    // 判断学生id是否已经存在
    public static boolean contains(ArrayList<Student> list, String id) {
       /* for (Student student : list) {
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;*/
        return getIndex(list, id) >= 0;
    }

    // 根据id查询学生信息，存在返回对应的索引，不存在返回-1
    public static int getIndex(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if (student.getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }
}
